package com.zoro.barn.business.service.system.business.role.controller;

import com.zoro.barn.business.service.system.business.role.service.BarnRoleService;
import com.zoro.barn.business.service.system.business.role.entity.BarnRole;
import com.zoro.barn.business.service.system.business.role.entity.BarnRoleMenuDto;
import com.zoro.barn.business.service.system.business.role.entity.BarnRoleUserDto;
import com.zoro.barn.business.service.system.business.teamuser.entity.BarnTeamUser;
import com.zoro.barn.business.service.system.business.teamuser.service.BarnTeamUserService;
import com.zoro.barn.commons.webcommons.tools.WebContentUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/role")
public class BarnRoleController {

    private final BarnRoleService barnRoleService;
    private final BarnTeamUserService barnTeamUserService;

    public BarnRoleController(BarnRoleService barnRoleService, BarnTeamUserService barnTeamUserService) {
        this.barnRoleService = barnRoleService;
        this.barnTeamUserService = barnTeamUserService;
    }

    /**
     * 主键查询
     *
     * @param pid 主键
     * @return result
     */
    @RequestMapping(value = "/getById/{pid}", method = RequestMethod.GET)
    public BarnRole getById(@PathVariable("pid") String pid) {
        return this.barnRoleService.getById(pid);
    }

    /**
     * 保存
     *
     * @param barnRole 保存
     * @return saved data
     */
    @RequestMapping("/save")
    public BarnRole save(@RequestBody BarnRole barnRole) {
        if (StringUtils.isBlank(barnRole.getPid())) {
            barnRole.setCreateId(WebContentUtil.requestUserInfo().getPid());
        } else {
            barnRole.setUpdateId(WebContentUtil.requestUserInfo().getPid());
        }

        return this.barnRoleService.save(barnRole);
    }

    /**
     * 条件查询
     *
     * @param param 查询条件
     * @return 查询结果
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public List<BarnRole> query(@RequestBody BarnRole param) {
        return this.barnRoleService.query(param);
    }

    /**
     * 主键删除
     *
     * @param pid pid
     */
    @RequestMapping("/delete/{pid}")
    public void delete(@PathVariable("pid") String pid) {

        if (StringUtils.isBlank(pid)) {
            return;
        }
        this.barnRoleService.delete(pid);
    }

    /**
     * 查找用户的所有角色
     *
     * @param userId userId
     * @return 角色
     */
    @RequestMapping(value = "/getByUserId/{userId}", method = RequestMethod.GET)
    public List<BarnRole> getByUserId(@PathVariable String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        return this.barnRoleService.getByUserId(userId);
    }

    /**
     * 查找用户的所有角色
     *
     * @param userId userId
     * @return 角色
     */
    @RequestMapping(value = "/getByUserTeam/{userId}", method = RequestMethod.GET)
    public List<BarnRole> getByUserTeam(@PathVariable String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }

        BarnTeamUser teamUser = new BarnTeamUser();
        teamUser.setUserId(userId);
        List<BarnTeamUser> teams = barnTeamUserService.query(teamUser);
        Set<String> teamIds = null;
        if (CollectionUtils.isNotEmpty(teams)) {
            teamIds = teams.stream().map(BarnTeamUser::getTeamId).collect(Collectors.toSet());
        }
        return this.barnRoleService.getByUserTeam(teamIds);
    }

    /**
     * 查询用户，并一并查询是否拥有某个角色
     *
     * @param dto {@link BarnRoleUserDto}
     * @return users
     */
    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    public List<BarnRoleUserDto> getUsers(BarnRoleUserDto dto) {

        return this.barnRoleService.getUsers(dto);

    }

    /**
     * 查询某个角色的菜单情况
     *
     * @param menuDto {@link BarnRoleMenuDto}
     * @return result
     */
    @RequestMapping(value = "/getMenus", method = RequestMethod.GET)
    public List<BarnRoleMenuDto> getMenus(BarnRoleMenuDto menuDto) {
        return this.barnRoleService.getMenus(menuDto);
    }


    /**
     * 查找 team 的所有角色
     *
     * @param teamId teamId
     * @return 角色
     */
    @RequestMapping(value = "/getByTeamId/{teamId}", method = RequestMethod.GET)
    public List<BarnRole> getByTeamId(@PathVariable String teamId) {
        if (StringUtils.isBlank(teamId)) {
            return null;
        }
        return this.barnRoleService.getByTeamId(teamId);
    }

}
